#include "odnmain.h"
#include "ui_odnmain.h"
#include "plotdisplay.h"
#include "plotdeny.h"
#include <QProcess>
#include <stdio.h>

unsigned int consumerStatus = 0;
unsigned int networkStatus = 0;
unsigned int subStatus = 0;

odnMain::odnMain(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::odnMain)
{
    ui->setupUi(this);
    if (consumerStatus==1)
        ui->label_consumerStatus->setText(tr("Consumer position status \tOK.."));
    else
        ui->label_consumerStatus->setText(tr("Consumer position status \twaiting.."));
    if (networkStatus==1)
        ui->label_NetworkStatus->setText(tr("NNetwork linking status \tOK.."));
    else
        ui->label_NetworkStatus->setText(tr("NNetwork linking status \twaiting.."));
    if (subStatus==1)
        ui->label_SubStatus->setText(tr("Substation location status \tOK.."));
    else
        ui->label_SubStatus->setText(tr("Substation location status \twaiting.."));



}

odnMain::~odnMain()
{
    delete ui;
}

void odnMain::on_generateBotton_clicked()
{
    int consumerNum = ui->consumerNumBox->value();
    int subNum = ui->subBox->value();
    float branchRate  = ui->branchRateBox->value();


    QStringList  list1;
    list1<<QString::number(consumerNum, 10)<<QString::number(branchRate);
    bool odnRun = QProcess::startDetached("./ODN",list1);

    QStringList  list2;
    list2<<"./V.txt"<<QString::number(consumerNum, 10)<<QString::number(subNum, 10)<<"10000";
    bool kmeansRun = QProcess::startDetached("./kmeans",list2);

    if (odnRun==TRUE)
    {
    consumerStatus = 1;
    networkStatus = 1;
    }
    if (kmeansRun==TRUE)
    subStatus = 1;

    if (consumerStatus==1)
        ui->label_consumerStatus->setText(tr("Consumer position status \tOK.."));
    else
        ui->label_consumerStatus->setText(tr("Consumer position status \twaiting.."));
    if (networkStatus==1)
        ui->label_NetworkStatus->setText(tr("NNetwork linking status \tOK.."));
    else
        ui->label_NetworkStatus->setText(tr("NNetwork linking status \twaiting.."));
    if (subStatus==1)
        ui->label_SubStatus->setText(tr("Substation location status \tOK.."));
    else
        ui->label_SubStatus->setText(tr("Substation location status \twaiting.."));


}



void odnMain::on_plotButton_clicked()
{
    if(consumerStatus == 1 &&  networkStatus == 1 &&  subStatus == 1)
    {
        plotDisplay *dlg = new plotDisplay(this);

        dlg->show();
    }
    else
    {
        plotDeny *dlg = new plotDeny(this);
        dlg->show();
    }

}

